﻿

using IDEAMS.core;
using IDEAMS.utils;
using log4net;
using System.IO;
using System.Threading;

namespace IDEAMS.model.result
{
    public class ProcessResult
    {
        private static ILog logger = LogManager.GetLogger(typeof(ProcessResult));

        public bool preDataAcquisition;
        public bool preEtalon;
        public bool preSpectrumGrating;
        public bool preSpectrumNoGrating;
        public bool continuoutDetection;
        public bool postSpectrumNoGrating;
        public bool postSpectrumGrating;
        public bool postEtalon;
        public bool postDataAcquisition;

        public ProcessResult()
        {
            preDataAcquisition = false;
            preEtalon = false;
            preSpectrumGrating = false;
            preSpectrumNoGrating = false;
            continuoutDetection = false;
            postSpectrumNoGrating = false;
            postSpectrumGrating = false;
            postEtalon = false;
            postDataAcquisition = false;
        }

        public void Reset()
        {
            preDataAcquisition = false;
            preEtalon = false;
            preSpectrumGrating = false;
            preSpectrumNoGrating = false;
            continuoutDetection = false;
            postSpectrumNoGrating = false;
            postSpectrumGrating = false;
            postEtalon = false;
            postDataAcquisition = false;
        }

        public ProcessResult(ProcessResult result)
        {
            this.preDataAcquisition = result.preDataAcquisition;
            this.preEtalon = result.preEtalon;
            this.preSpectrumGrating = result.preSpectrumGrating;
            this.preSpectrumNoGrating= result.preSpectrumNoGrating;
            this.continuoutDetection = result.continuoutDetection;
            this.postSpectrumNoGrating = result.postSpectrumNoGrating;
            this.postSpectrumGrating = result.postSpectrumGrating;
            this.postEtalon = result.postEtalon;
            this.postDataAcquisition = result.postDataAcquisition;
        }

        private void Copy(ProcessResult result)
        {
            this.preDataAcquisition = result.preDataAcquisition;
            this.preEtalon = result.preEtalon;
            this.preSpectrumGrating = result.preSpectrumGrating;
            this.preSpectrumNoGrating = result.preSpectrumNoGrating;
            this.continuoutDetection = result.continuoutDetection;
            this.postSpectrumNoGrating = result.postSpectrumNoGrating;
            this.postSpectrumGrating = result.postSpectrumGrating;
            this.postEtalon = result.postEtalon;
            this.postDataAcquisition = result.postDataAcquisition;
        }

        public ProcessResult Clone()
        {
            return new ProcessResult(this);
        }

        public int Load(string serialNo)
        {
            int rn = 0;

            string dataFile = string.Format(TW_App.fileManager.resultFile, serialNo);
            bool existsFlag = File.Exists(dataFile);
            if(!existsFlag)
            {
                Reset();
                return -1;
            }

            ProcessResult processResult = XmlUtil.Load<ProcessResult>(dataFile);
            if(processResult != null)
            {
                Copy(processResult);
                rn = 0;
            }
            else
            {
                Reset();
                rn = -1;
            }
            return rn;
        }

        public bool Save(string serialNo)
        {
            string dataFile = string.Format(TW_App.fileManager.resultFile, serialNo);

            ProcessResult processResult = new ProcessResult(this);
            bool rn = XmlUtil.Save<ProcessResult>(dataFile, processResult);
            return rn;
        }
    }
}
